package com.zHan.sky.controller.User;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zHan.sky.common.result.Result;
import com.zHan.sky.dto.DishDto;
import com.zHan.sky.entity.Category;
import com.zHan.sky.entity.Dish;
import com.zHan.sky.entity.DishFlavor;
import com.zHan.sky.service.CategoryService;
import com.zHan.sky.service.DishFlavorService;
import com.zHan.sky.service.DishService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 菜品管理
 */
@Slf4j
@RestController("userDishController")
@RequestMapping("/user/dish")
@Api(tags = "C端-菜品相关接口")
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private RedisTemplate redisTemplate;


    /**
     * 根据条件查询菜品数据
     *
     * @param dish
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "根据条件查询菜品数据")
    @Cacheable(value = "dishCache", key = "#dish.categoryId")
    public Result<List<DishDto>> list(Dish dish) {

        List<DishDto> dishDtoList = null;
//        String key = "dish_" + dish.getCategoryId() + "_" + dish.getStatus();
//        //在redis中查询有没有数据
//        dishDtoList = (List<DishDto>) redisTemplate.opsForValue().get(key);

        if (dishDtoList != null) {
            //若存在，直接获取
            return Result.success(dishDtoList);
        }

        //条件构造器
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        //过滤条件
        queryWrapper.eq(dish.getCategoryId() != null, Dish::getCategoryId, dish.getCategoryId());
        //只查询启售的菜品，status=1
        queryWrapper.eq(Dish::getStatus, 1);
        //排序条件
        queryWrapper.orderByDesc(Dish::getUpdateTime);
        //执行方法
        List<Dish> dishList = dishService.list(queryWrapper);

        dishDtoList = dishList.stream().map((item) -> {
            //创建dishDto
            DishDto dishDto = new DishDto();
            //对象拷贝
            BeanUtils.copyProperties(item, dishDto);
            //获得categoryId
            Long categoryId = item.getCategoryId();
            Category category = categoryService.getById(categoryId);

            if (category != null) {
                String categoryName = category.getName();
                dishDto.setCategoryName(categoryName);
            }

            //获得菜品id
            Long id = item.getId();
            //根据菜品id查到对应口味
            LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(DishFlavor::getDishId, id);
            List<DishFlavor> dishFlavorList = dishFlavorService.list(lambdaQueryWrapper);

            dishDto.setFlavors(dishFlavorList);

            return dishDto;
        }).collect(Collectors.toList());

//        //若不存在，添加数据到redis
//        redisTemplate.opsForValue().set(key, dishDtoList, 1, TimeUnit.HOURS);

        return Result.success(dishDtoList);
    }

}
